/*******************************************************************/
/*    Copyright (c) 1999-2020 by Spatial Corp.                     */
/*    All rights reserved.                                         */
/*    Protected by U.S. Patents 5,257,205; 5,351,196; 6,369,815;   */
/*                              5,982,378; 6,462,738; 6,941,251    */
/*    Protected by European Patents 0503642; 69220263.3            */
/*    Protected by Hong Kong Patent 1008101A                       */
/*******************************************************************/

//----------------------------- IopAcisDocumentInfo.cpp -------------------------------

#include <iostream>

//Acis
#include "kernapi.hxx" 
#include "lists.hxx"

//Iop
#include "SPAIopAcis.h"
#include "SPAIopArgParser.h"
#include "SPAIopAcisSampleUtilities.h"
#include "SPAIopDocumentInfoUtilities.h"
#include "SPAIopPerformanceStatistics.h"

using namespace SPAIopAcisPrintUtils;

// License file
#include "spatial_license.h"
#include "spa_unlock_result.hxx"
//-----------------------------------------------------------------------------------------
void PrintHelp()
{
	PrintMessage(L"Usage: IopAcisDocumentInfo -i <full-input-file-path> [-o <full-output-dump-file-path>] [-h] \n");
	PrintMessage(L"\t-i  : The absolute path to the input file. \n");
	PrintMessage(L"\t-o  : The absolute path to the output dump file(.txt). \n");
	PrintMessage(L"\t-h  : This help.\n");
}
//-----------------------------------------------------------------------------------------
int test_IopAcisDocumentInfo(int argc, char* argv[])
{
	int result(1);
	SPAIopPerformanceStatistics printPerfoStatsObj;

	// Work-flow similar to Acis based app where Acis will be generally initialized before IOP is called.
	api_start_modeller(0);

	try
	{
		char* argFields[] = { "i", "o", "h" };
		int size = sizeof(argFields) / sizeof(*argFields);
		SPAIopArgParser parser(size, argFields, argc, argv, &PrintHelp);
		// IOP is called in its own scope.
		// This sample uses a guard class that initializes InterOp upon 
		// construction and terminates InterOp upon destruction.  All 
		// translations are done within the boundaries of a single 
		// initialization and shutdown.  InterOp may only be initialized 
		// and shutdown once per process.  Please do not use the guard 
		// classes multiple times per process.
		SPAIopAcisSystemInitGuard initGuard(SPATIAL_LICENSE);

		result = initGuard.GetInitStatus();

		if (SPA_UNLOCK_NO_MSG == result)
		{
			// Spatial products have been unlocked
			std::wcout << L"SUCCESS: SPAIopAcisSystem::Initialize. " << std::endl;
		}
		else if (SPA_UNLOCK_KEY_WARN_EXPIRE == result)
			std::wcout << L"SPAIopAcisSystem::Initialize returned SPA_UNLOCK_KEY_WARN_EXPIRE; your license key will expire soon!\n";
		else if (SPA_UNLOCK_REGISTRATION_WARN_EXPIRE == result)
			std::wcout << L"SPAIopAcisSystem::Initialize returned SPA_UNLOCK_REGISTRATION_WARN_EXPIRE; your registration will expire soon.\n";
		else
		{
			std::wcout << L"SPAIopAcisSystem::Initialize returned an unexpected value; Contact Spatial Customer Support or your Account Manager.\n";
			return 1;
		}
		result = 1;

		

		if (true == parser.IsSuccess())
		{
			const wchar_t* pSourceFilePath = NULL;
			bool hasInputFileName = parser.GetWStringValueFor("i", pSourceFilePath);

			const wchar_t* pOutputFilePath = NULL;
			bool hasOutputFileName = parser.GetWStringValueFor("o", pOutputFilePath);

			if (true == hasOutputFileName)
			{
				if (false == OpenFileForConsoleOutput(pOutputFilePath))
				{
					return 11;
				}
			}

			if (hasInputFileName && pSourceFilePath)
			{
				bool success(false);
				try
				{
					success = AccessDocumentInformation(pSourceFilePath);
				}
				catch (SPAIopAcisError& pError)
				{
					DisplayError(pError);
					PrintMessage(L"Caught SPAIopAcisError while accessing Information on Document \n");
					success = false;
				}
				catch (...)
				{
					PrintMessage(L"FAILURE: Unknown exception caught! \n");
					success = false;
				}

				if (true == success)
				{
					result = 0;
				}
			}

			if (true == hasOutputFileName)
			{
				CloseFileForConsoleOutput();
			}
		}
	}
	catch (SPAIopAcisError& fatalErr)
	{
		DisplayError(fatalErr);
		result = 1;
	}
	catch (...)
	{
		PrintMessage(L"FAILURE: Unknown exception caught! ");
		result = 1;
	}

	api_stop_modeller();

	return result;
}

//-----------------------------------------------------------------------------------------

